package com.star.rpc.serializer;

import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * packageName com.star.rpc.serializer
 *
 * @author zerostart
 * @className HessianSerializer
 * @date 2024/11/20
 * @description
 */
public class HessianSerializer implements Serializer {
    @Override
    public <T> byte[] serialize(T object) throws Exception {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        HessianOutput output = new HessianOutput(byteArrayOutputStream);
        output.writeObject(object);
        return byteArrayOutputStream.toByteArray();
    }

    @Override
    public <T> T deserialize(byte[] bytes, Class<T> clazz) throws Exception {
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        HessianInput input = new HessianInput(byteArrayInputStream);

        return (T) input.readObject(clazz);
    }
}
